package com.qf.service.impl;

import com.qf.pojo.Order;
import com.qf.service.OrderService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

//@Service
/*
    通过dubbo下的@Service 注册，将该服务暴露出去

    @Service 作用：
        ① 将当前对象添加到spring容器中（ServiceBean-OrderServiceImpl）
        ② 服务导出
            1、根据服务协议开启对应的服务器（dubbo协议-netty服务；http协议-tomcat）
            2、根据注册证中心，将服务组织成一个url写入到注册中心保存
 */
//@Service(timeout = 2000)
//@Service(version = "2.0")
@Service
public class OrderServiceImpl implements OrderService {

    @Value("${server.port}")
    private Integer port;

    @Override
    public List<Order> findByUid(Integer uid) throws InterruptedException {

//        if (uid == 1){

        //模拟集群容错测试
        if (port == 81){
            Thread.sleep(3000);
        }

        System.out.println("业务继续执行...");

        //根据用户id，数据库查询用户的订单信息
        Order order1 = new Order(1,uid,1000d,"用户丁1:" + port);
        Order order2 = new Order(2,uid,2000d,"用户丁2:" + port);
        Order order3 = new Order(3,uid,3000d,"用户丁3:" + port);

        List<Order> orders = new ArrayList<>();
        orders.add(order1);
        orders.add(order2);
        orders.add(order3);

        return orders;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String url = "dubbo%3A%2F%2F192.168.247.1%3A20881%2Fcom.qf.service.OrderService%3Fanyhost%3Dtrue%26application%3Dqf-order%26bean.name%3DServiceBean%3Acom.qf.service.OrderService%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.qf.service.OrderService%26methods%3DfindByUid%26pid%3D12176%26register%3Dtrue%26release%3D2.7.3%26side%3Dprovider%26timestamp%3D1694401048585";
        String decode = URLDecoder.decode(url, "utf-8");
        System.out.println(decode);
    }
}
